package osb.map.main;

import osb.map.database.ConfigurationRW;
import osb.map.net.*;
import osb.map.publicdata.Public_Operate_And_Data;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.ljp.ani.R;

/**
 * 登录 activity
 * @author Administrator
 *
 */

public class Login_Main extends Activity implements View.OnClickListener
{

    public static Login_Main Public_Static_LoginMain = null;
    public String String_username;
    public String String_userpassword;
    public static boolean IsSavaPassword = false; // 是否保存密码

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 已经登陆 - 网络连接正确 直接跳转
        if (Public_Operate_And_Data.Islogin && Public_Operate_And_Data.IsConnectSuccess)
        {
            Intent intent = new Intent();
            intent.setClass(Login_Main.this, Login_Accessed.class);
            startActivity(intent);
            Login_Main.this.finish();
            return;
        }
        setContentView(R.layout.login_main);
        Public_Static_LoginMain = this;

        final Button button_login = (Button) findViewById(R.id.login);
        final Button button_regist = (Button) findViewById(R.id.regist);
        final EditText usernameEditText = (EditText) findViewById(R.id.username);
        final EditText userpasswordEditText = (EditText) findViewById(R.id.userpassword);

        usernameEditText.setOnClickListener(this);
        userpasswordEditText.setOnClickListener(this);

        // 登录按钮
        button_login.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String_username = usernameEditText.getText().toString();         // 获得账号
                String_userpassword = userpasswordEditText.getText().toString(); // 获得密码
                if (String_username.equals(""))
                {
                    Toast.makeText(Login_Main.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if (String_userpassword.equals(""))
                {
                    Toast.makeText(Login_Main.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 网络连接失败
                if (!Public_Operate_And_Data.IsConnectSuccess) 
                {
                    new AlertDialog.Builder(Login_Main.this)
                    .setTitle("登陆失败").setMessage("网络连接不可用")
                    .setPositiveButton("确定", null).show();
                    return;
                }

                Public_Operate_And_Data.user_pwd = String_userpassword; // 用户密码赋值到全局 
                DataPackage data = new DataPackage(msgType.Login_USer); // 定义为用户登录消息类型
                data.dataUser = new DataUser(String_username, String_userpassword); // 添加数据包内容 用户名和密码
                ClientSocket.Public_Static_ClientSocket.sendMsg(data);  // 发送数据包
                Toast.makeText(Login_Main.this, "登录中", Toast.LENGTH_SHORT).show();
                button_login.setClickable(false); 
            }
        });
        // 注册按钮
        button_regist.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setClass(Login_Main.this, RegisterActivity.class); 
                startActivity(intent);
            }
        });
    }

    /**
     * 登录成功
     */
    public void loginSuccess()
    {
        CheckBox checkBox = (CheckBox) findViewById(R.id.auto_save_password);
        // 记住密码
        if (checkBox.isChecked())
        {
        	// 标志为自动登录 - 写入配置文件
            Public_Operate_And_Data.IsAutoLogin = true; 
            ConfigurationRW.Configuration_W_this("true",
                                                 Public_Operate_And_Data.User_id, null,
                                                 Public_Operate_And_Data.User_name, ""
                                                 + Public_Operate_And_Data.User_accout, this);  
            }
        Intent intent = new Intent();
        intent.setClass(Login_Main.this, Login_Accessed.class);  
        startActivity(intent);
        Login_Main.this.finish();
    }

    /**
     * 登录失败
     */
    public void loginUserPwdWrong()
    {
        final Button button_login = (Button) findViewById(R.id.login);
        button_login.setClickable(true);
        new AlertDialog.Builder(Login_Main.Public_Static_LoginMain)
        .setTitle("错误").setMessage("登陆失败了")
        .setPositiveButton("确定", null).show();
    }

	@Override
	public void onClick(View v) {
		
	}
	
	/**
	 * menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 3, "分享下载").setIcon(android.R.drawable.ic_menu_send);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Intent intent = new Intent();
			intent.setClass(Login_Main.this, ShareActivity.class);
			Login_Main.this.startActivity(intent);

			break;
		}
		return false;
	}

}